from selenium.webdriver.common.by import By
# from app_package import PACKAGE
from page.net_project_page import NetProjectPage
from common.page import IOS, XPATH
import time as t
import random as r



class NetLivePage(NetProjectPage):
    Origin =(IOS,'type == "XCUIElementTypeTextView" AND lable="请输入内容"')
    comment_text = (XPATH, '//XCUIElementTypeStaticText[@name="请输入内容"]/../XCUIElementTypeTextView')

    # 时间选择框
    Minute = (XPATH,"//XCUIElementTypePickerWheel[4]")

    hour = t.strftime("%H",t.localtime(t.time()))
    def publish_live(self, title="App直播自动化测试%d" % r.randint(100, 999)):
        t.sleep(5)
        self.find_button(string="发布",contains_flag=1).click()
        self.find_element(XPATH, "//XCUIElementTypeTextView[starts-with(@value,'直播标题')]").send_keys(title)
        self.find_static_text("开始时间").click()
        # self.swipe(310,540,310,500)    # 机型6，直接放开   注释下边时间

        NewMinute = int(t.strftime('%M', t.localtime()))
        self.get_frame_center_and_swipe(self.Minute)  #6S机型

        if NewMinute >=58:
            self.find_element(XPATH,"//XCUIElementTypePickerWheel[3]").send_keys("hour"+1)

        self.find_button("确定").click()
        self.find_static_text("直播时长（分钟）").click()
        t.sleep(1)
        self.find_element(XPATH, "//XCUIElementTypeTextField[@value='0']").send_keys("300")
        self.find_button("确定").click()
        self.find_element(XPATH, "//XCUIElementTypeTextField[contains(@value,'请使用数字')]").send_keys("123123")
        self.hide_keyboard()
        self.find_static_text("重复登录设置").click()
        self.get_frame_center_and_swipe((XPATH, "//XCUIElementTypePickerWheel[@value='不限']"))
        self.find_button("确定").click()
        self.find_static_text("收款方式").click()
        self.get_frame_center_and_swipe((XPATH, "//XCUIElementTypePickerWheel[@value='支付宝']"))
        t.sleep(1)
        self.find_button("确定").click()
        self.swipe_dr('up')
        self.find_element(
            XPATH, "//XCUIElementTypeTextView[starts-with(@value,'直播简介')]"
        ).send_keys("这是直播简介的内容。不要因为命运的怪诞而俯首听命于它，任凭它的摆布。等你年老的时候，回首往事，就会发觉，命运有一半在你手里，只有另一半才在上帝的手里。你一生的全部就在于：运用你手里所拥有的去获取上帝所掌握的。这就是命运的一生；这就是一生的命运。")
        self.find_button("发布").click()

    def start_live(self, title="App直播自动化测试"):
        t.sleep(5)
        self.find_element(IOS, "type == 'XCUIElementTypeOther' and label CONTAINS '%s'" % title).click()
        t.sleep(2)
        self.find_button(string="去评论", contains_flag=1).click()
        self.swipe(20,699,20,699)

        self.find_element(*self.comment_text).send_keys("直播评论内容。不要因为命运的怪诞而俯首听命于它，任凭它的摆布。等你年老的时候，回首往事，就会发觉，命运有一半在你手里，只有另一半才在上帝的手里。你一生的全部就在于：运用你手里所拥有的去获取上帝所掌握的。你的努力越超常，在你彻底绝望的时候用你自己的一半去获取上帝手中的一半。 这就是命运的一生；这就是一生的命运。")
        self.find_button("发布").click()
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        t.sleep(3)
        self.find_button("开启直播").click()
        for i in range(2):
            t.sleep(10)
            self.find_button("切换摄像头").click()
        t.sleep(5)
        # self.find_element(XPATH, "//XCUIElementTypeButton[contains(@name,'close']").click()  # 点击元素时报错，暂未解决
        self.find_button(string="close",contains_flag=1).click()
        self.find_button("确定").click()
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        t.sleep(0.5)
        self.find_button("未报名").click()
        self.swipe_dr('down')
        t.sleep(0.5)
        self.find_button("已报名").click()
        self.swipe_dr('down')
        t.sleep(0.5)
        self.find_button("已参与").click()
        self.swipe_dr('down')
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="common back light icon", contains_flag=1).click()

    def end_live(self,title="直播自动化测试"):  #   项目是62481

        self.swipe_dr("down")
        t.sleep(3)
        # self.find_element(XPATH,"//XCUIElementTypeButton[contains(name='已结束')]").click()     #通过已结束按钮
        self.find_element(IOS, "type == 'XCUIElementTypeOther' and label CONTAINS '%s'" % title).click()  #通过名称来找
        self.find_button("免费报名").click()
        t.sleep(3)
        self.find_button("观看直播").click()
        t.sleep(3)
        self.find_button("我知道了").click()
        self.find_static_text(string="在这里发送信息哦").click()
        self.find_static_text(string="在这里发送信息哦").send_keys("今天讲课的内容很不错，聊天记录可以互相学习一下")
        self.find_button("发送").click()
        t.sleep(3)
        self.find_button("问答").click()
        self.find_button("简介").click()
        self.find_static_text("直播未开始").click()
        # self.swipe(40,80,40,80)
        # self.find_button("直播返回返回").click()
        self.find_button("nav ic back nor white").click()
        self.find_element(XPATH,"//XCUIElementTypeOther[3]/XCUIElementTypeButton[1]").click() #确定
        # self.swipe(210,377,210,377)   #确定
        t.sleep(3)
        self.find_button(string="返回", contains_flag=1).click()
        t.sleep(1)
        self.find_button(string="返回", contains_flag=1).click()
        self.find_button(string="common back light icon", contains_flag=1).click()